package com.qut.atcoder.service.impl;

import com.qut.atcoder.mappers.QuestionMapper;
import com.qut.atcoder.pojo.Question;
import com.qut.atcoder.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    private final String path = "C:\\Users\\Public\\Documents\\atcode\\question\\";

    @Autowired(required = false)
    private QuestionMapper questionMapper;

    @Override
    public List<Question> getQuestionById(Question question) {
        List<Question> list = questionMapper.selectQuestionById(question);
        for (Question it : list) {
            int id = it.getId();
            int in = 0;
            int out = 0;
            String[] inputs = new String[10];
            String[] outputs = new String[10];
            File file = new File(path + id + "\\");
            File[] files = file.listFiles();
            if (files == null)
                return null;
            for (File io : files) {
                if (io.isFile()) {
                    if (io.getName().endsWith(".in")) {
                        in++;
                        try {
                            inputs[Integer.parseInt(io.getName().substring(0,1))] =
                                    new String(Files.readAllBytes(Paths.get(io.getPath())));
                        } catch (IOException e) {
                            e.printStackTrace();
                            return null;
                        }
                    } else if (io.getName().endsWith(".out")) {
                        out++;
                        try {
                            outputs[Integer.parseInt(io.getName().substring(0,1))] =
                                    new String(Files.readAllBytes(Paths.get(io.getPath())));
                        } catch (IOException e) {
                            e.printStackTrace();
                            return null;
                        }
                    }
                } // if (io.isFile())
            } // for (File io : files)
            if (in != out && (out != 1 || in != 0))
                return null;
            if (out == 0)
                return null;
            it.resizeInput_test(in);
            it.resizeOutput_test(out);
            for (int i = 0; i < in; i++)
                it.getInput_test()[i] = inputs[i];
            for (int i = 0; i < out; i++)
                it.getOutput_test()[i] = outputs[i];
        }
        return list;
    }

    @Override
    public int deleteQuestionById(Question question) {
        List<Question> list = questionMapper.selectQuestionById(question);
        if (list.size() < 1)
            return 1;
        question = list.get(0);
        question.setIs_exist(0);
        int ret = questionMapper.updateQuestionById(question);
        if (ret != 1)
            return 2;
        return 0;
    }


    @Override
    public int setQuestionById(Question question) {
        int ret = questionMapper.updateQuestionById(question);
        if (ret != 1)
            return 1;
        return 0;
//        if (question.getOutput_test().length == 0)
//        if (question.getInput_test().length != question.getOutput_test().length &&
//                (question.getInput_test().length != 0 || question.getOutput_test().length != 1)) {
//            return 2;
//        }
//        String workFile = path + question.getId() + "\\";
//        for (int i = 0; i < question.getInput_test().length; i++) {
//            File file = new File(workFile + i + ".in");
//            try {
//                Files.write(Paths.get(file.getPath()),question.getInput_test()[i].getBytes());
//            } catch (IOException e) {
//                e.printStackTrace();
//                return 3;
//            }
//        }
//        for (int i = 0; i < question.getOutput_test().length; i++) {
//            File file = new File(workFile + i + ".out");
//            try {
//                Files.write(Paths.get(file.getPath()),question.getInput_test()[i].getBytes());
//            } catch (IOException e) {
//                e.printStackTrace();
//                return 4;
//            }
//        }
//        return 0;
    }

    @Override
    public int createQuestion(Question question) {
        int num = questionMapper.insertQuestion(question);
        String workFile = path + num + "\\";
        for (int i = 0; i < question.getInput_test().length; i++) {
            File file = new File(workFile + i + ".in");
            try {
                Files.write(Paths.get(file.getPath()),question.getInput_test()[i].getBytes());
            } catch (IOException e) {
                e.printStackTrace();
                return 0;
            }
        }
        for (int i = 0; i < question.getOutput_test().length; i++) {
            File file = new File(workFile + i + ".out");
            try {
                Files.write(Paths.get(file.getPath()),question.getInput_test()[i].getBytes());
            } catch (IOException e) {
                e.printStackTrace();
                return 0;
            }
        }
        return num;
    }
}
